#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/io.h>

#include "../usart.h"
#include "../lcd.h"
#include "../util.h"

#define usart_tx_str(dev, s) (usart_tx((dev), (uint8_t *) (s), strlen((s))))

int
main(void)
{
	char buf[128];
	int len;
	int i;

	lcd_init();
	usart_init();
	sei();

#if 0
	usart_tx_str(0, "start tx test:\r\n");
	for (i = 0; i < 512; ++i) {
		sprintf(buf, "hello world %d\r\n", i);
		j = usart_tx_str(0, buf);
		if (j != strlen(buf)) {
			sprintf(buf, "tx: %d, i: %d", j, i);
			fatal(buf);
		}
		_delay_ms(1);
	}
	_delay_ms(1000);
	usart_tx_str(0, "start rx test:\r\n");
#endif

	for (;;) {
		len = usart_rx(0, (uint8_t *) buf, sizeof(buf) - 1);
		buf[len] = '\0';
		usart_puts(1, buf);

		len = usart_rx(1, (uint8_t *) buf, sizeof(buf) - 1);
		buf[len] = '\0';
		usart_puts(0, buf);
	}

	return 0;
}
